package com.panshi.controller;

import com.panshi.service.IAppEssayService;
import com.panshi.tools.AppEssay;
import com.panshi.tools.Check;
import com.panshi.tools.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@ResponseBody
// 跨域
@CrossOrigin
public class AppEssayController {

    @Autowired
    private IAppEssayService iAppEssayService;

    // Redis
//    Jedis jedis = new Jedis("localhost",6379);

    /**
     * 推荐 查前十条
     * @return
     */
    @RequestMapping("/queryAppEssay")
    public List queryAppEssay(int page,Integer topicId){
        int a = this.page(page);
        Paging p = new Paging();
        p.setPage(a);
        p.setTopicId(topicId);
       List<AppEssay> list =  iAppEssayService.queryAppEssay(p);
        for (AppEssay app: list) {
            app.setTime(this.getDatePoor(app.getGroundTime()));
        }
        log.error("推荐 查前十条");
       return  list;
    }

    /***
     * 热榜 根据阅读量  十条 降序
     * @return
     */
    @RequestMapping("/queryAppEssayRb")
    public List queryAppEssayRb(int page){
        int a = this.page(page);
        List<AppEssay> list =  iAppEssayService.queryAppEssayRb(a);
        for (AppEssay app: list) {
            app.setTime(this.getDatePoor(app.getGroundTime()));
        }
        log.error("热榜 根据阅读量  十条 降序");
        return  list;
    }

//    /**
//     * 图文热搜
//     */
//    @RequestMapping("/zx/queryAppEssayTwRb")
//    public List queryAppEssayTwRb(){
//
//        if(null == jedis.zrevrange("queryAppEssay",0,3)){
//            List<AppEssay> list = iAppEssayService.queryAppEssayTwRb();
//            for (AppEssay appEssay:list) {
//                String app = JSONObject.toJSONString(appEssay);
//                jedis.zadd("queryAppEssay",0,app);
//            }
//            return  list;
//        }else{
//            Set<String> set =  jedis.zrevrange("queryAppEssay",0,3);
//            for (String str: set) {
//                System.out.println("-------      "+str);
//            }
//            List list = new ArrayList(set);
//            return  list;
//        }
//    }



    /***
     * 最新  根据时间 十条 升序
     * @return
     */
    @RequestMapping("/queryAppEssayNew")
    public List queryAppEssayNew(int page){
        int a = this.page(page);
        List<AppEssay> list =  iAppEssayService.queryAppEssayNew(a);
        for (AppEssay app: list) {
            app.setTime(this.getDatePoor(app.getGroundTime()));
        }
        log.error("最新  根据时间 十条 升序");
        return  list;
    }



    /***
     * 最新创作 根据上架时间  降序
     * @return
     */
    @RequestMapping("/queryAppEssayNewCz")
    public List queryAppEssayNewCz(int page){
        int a = this.page(page);
        List<AppEssay> list =  iAppEssayService.queryAppEssayNewCz(a);
        return  list;
    }

    /***
     * 根据用户id查询所有咨询
     * @param page
     * @param userId
     * @return
     */
    @RequestMapping("/queryAppEssayByUserId")
    public List queryAppEssayByUserId(int page , int userId){
        log.error("queryAppEssayByUserId   =>      page:"+page+"      userId:"+userId);
        Paging p = new Paging();
        p.setPage(page);
        p.setUserId(userId);
        List<Check> lis = iAppEssayService.queryAppEssayByUserId(p);
        return lis;
    }



    /***
     * 分页方法
     * @param page
     * @return
     */
    public int page (int page){
        if (page == 0) {
            return  0;
        } else if (page < 0){
            return  0;
        }else {
            return 10 * (page-1);
        }
    }

    public String getDatePoor(Date endDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long diff =  new Date().getTime()- endDate.getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        return day + "天" + hour + "小时" + min + "分钟";

    }

}